//
//  WodeViewController.m
//  BlueTDevice
//
//  Created by MingmingSun on 16/8/31.
//  Copyright © 2016年 Sunmingming. All rights reserved.
//

#import "WodeViewController.h"
#import "UserInfoViewController.h"
#import "LoginViewController.h"
#import <FUIButton.h>

@interface WodeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak) IBOutlet UIImageView *headImageView;
@property(nonatomic,weak) IBOutlet UILabel *nameLabel;
@property(nonatomic,weak) IBOutlet UIButton *loginButton;
@property(nonatomic,weak) IBOutlet UIButton *logoutButton;
@property(nonatomic,weak) IBOutlet UITableView *tableView;

-(IBAction)loginPressed:(id)sender;
-(IBAction)logoutPressed:(id)sender;

@end

@implementation WodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self.loginButton ]
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self drawUIByLoggedIn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.0f;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 1;
        case 1:
            return 2;
        default:
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0f;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier = @"characteristicCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                      reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:{
                    cell.textLabel.text = @"我的计划";
                    cell.imageView.image = [UIImage imageNamed:@"wodejihua"];
                }
                    break;
                case 1:{
                    cell.textLabel.text = @"我的设备";
                    cell.imageView.image = [UIImage imageNamed:@"wodeshebei"];
                }
                    break;
                default:
                    break;
            }
            break;
        case 1:
            /*
            switch (indexPath.row) {
                case 0:{
                    cell.textLabel.text = @"设置";
                    cell.imageView.image = [UIImage imageNamed:@"shezhi"];
                }
                    break;
                case 1:{
                    cell.textLabel.text = @"我的设备";
                    cell.imageView.image = [UIImage imageNamed:@"wodeshebei"];
                }
                    break;
                default:
                    break;
            }
             */
            switch (indexPath.row) {
                case 0:{
                    cell.textLabel.text = @"关于我们";
                    cell.imageView.image = [UIImage imageNamed:@"shezhi"];
                }
                    break;
                case 1:{
                    cell.textLabel.text = @"当前版本";
                    cell.detailTextLabel.text = @"1.0.0";
                    cell.imageView.image = [UIImage imageNamed:@"wodeshebei"];
                }
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:{
                }
                    break;
                case 1:{
                }
                    break;
                default:
                    break;
            }
            break;
        case 1:
            switch (indexPath.row) {
                case 0:{
                    UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:@"健康是一款很好的APP" preferredStyle:UIAlertControllerStyleAlert];
                    //    vc.view.backgroundColor = [UIColor goldColor];
                    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleCancel handler:nil];
                    [vc addAction:cancelAction];
                    [self presentViewController:vc animated:YES completion:nil];
                }
                    break;
                case 1:{
                }
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
}

-(IBAction)loginPressed:(id)sender{
    if(APPALL.myUserItem.username.length){
        //enter userinfo
        UserInfoViewController *vc = [UserInfoViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        /*
        APPALL.iHasLoggedIn = YES;
        [self drawUIByLoggedIn];
         */
        [APPALL showLoginWindow];
    }
}

-(IBAction)logoutPressed:(id)sender{
    [SVProgressHUD showWithStatus:@"loading..."];
    [APPALL.netSession GET:[NSString stringWithFormat:@"%@%@",APIURL,APILogout]
                 parameters:nil
                   progress:nil
                    success:^(NSURLSessionDataTask * task, id responseObject){
                        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                        NSLog(@"JSON: %@", JSON);
                        if([JSON[@"logout"] isEqualToString:@"success"]){
                            APPALL.myUserItem.username = @"";
                            APPALL.myUserItem.password = @"";
                            [self drawUIByLoggedIn];
                            [SVProgressHUD dismiss];
                        }else{
                            [SVProgressHUD showErrorWithStatus:@"注销失败"];
                        }
                    }
                    failure:^(NSURLSessionDataTask * task, NSError * error){
                        [SVProgressHUD dismiss];
                        //[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"请求失败:%@", error.description]];
                    }];
}

-(void)drawUIByLoggedIn{
    if(!APPALL.myUserItem.username.length){
        self.headImageView.image = [UIImage imageNamed:@"tab5.png"];
        self.nameLabel.text = @"";
        [self.loginButton setTitle:@"请登录" forState:UIControlStateNormal];
        self.logoutButton.hidden = YES;
    }else{
        self.headImageView.image = [UIImage imageNamed:@"icon_profile_003.png"];
        self.nameLabel.text = APPALL.myUserItem.nickname.length?APPALL.myUserItem.nickname:APPALL.myUserItem.username;
        [self.loginButton setTitle:@"个人信息 >" forState:UIControlStateNormal];
        self.logoutButton.hidden = NO;
    }
}

@end
